//
//  CYBindCardNormalCell.m
//  cyjf
//
//  Created by tauCross on 2017/5/19.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYBindCardNormalCell.h"



@implementation CYBindCardNormalCellObject

@end



@interface CYBindCardNormalCell ()

@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UITextField *contentTextField;
@property(nonatomic, strong)UIView *bottomLine;

@property(nonatomic, strong)CYBindCardNormalCellObject *object;

@end


@implementation CYBindCardNormalCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textColor = HEXCOLOR(@"444444");
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:self.titleLabel];
        
        self.contentTextField = [[UITextField alloc] init];
        self.contentTextField.textColor = HEXCOLOR(@"444444");
        self.contentTextField.font = [UIFont systemFontOfSize:16];
        self.contentTextField.returnKeyType = UIReturnKeyDone;
        [self.contentTextField addTarget:self.contentTextField action:@selector(resignFirstResponder) forControlEvents:UIControlEventEditingDidEndOnExit];
        [self.contentTextField addTarget:self action:@selector(onContentTextFieldEditingChanged) forControlEvents:UIControlEventEditingChanged];
        [self.contentView addSubview:self.contentTextField];
        
        self.bottomLine = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        [self.contentView addSubview:self.bottomLine];
    }
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 50;
}

- (BOOL)shouldUpdateCellWithObject:(id)object
{
    self.object = object;
    
    self.titleLabel.text = self.object.title;
    
    self.contentTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.object.placeholder attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16], NSForegroundColorAttributeName : HEXCOLOR(@"999999")}];
    self.contentTextField.keyboardType = self.object.keyboardType;
    self.contentTextField.text = self.object.content;
    
    self.bottomLine.hidden = self.object.isBottom;
    
    self.contentTextField.enabled = !self.object.lockInput;
    
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.titleLabel sizeToFit];
    self.titleLabel.centerY = self.contentView.height / 2;
    self.titleLabel.left = 20;
    
    self.contentTextField.width = self.contentView.width - 114 - 20;
    self.contentTextField.height = self.contentView.height;
    self.contentTextField.left = 114;
    self.contentTextField.top = 0;
    
    self.bottomLine.width = self.contentView.width - 20;
    self.bottomLine.height = 1 / SCREEN_SCALE;
    self.bottomLine.bottom = self.contentView.height;
    self.bottomLine.right = self.contentView.width;
}

#pragma mark - Control Events
- (void)onContentTextFieldEditingChanged
{
    self.object.content = self.contentTextField.text;
}

@end
